﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace Netra
{
    class Program
    {


        [STAThread]
        static void Main(string[] args)
        {
            LoadMerchandises();
 
            Galaxy gal = new Galaxy();
            gal.Generate(2);
            //Console.WriteLine(gal); // Wywaliłem, powinno ewentualnie przejść do TUI.cs
           
            TextUserInterface tui= new TextUserInterface(gal);
            TemplateMenuForm fui = new TemplateMenuForm(gal);

            Fleet fleet = new Fleet(new Position(0.0, 1.0));
            gal.Fleets.Add(fleet);

            //fui.Show();
            fui.Show();
            //fui.ShowDialog();
            //tui.Run();
            Application.Run();

            ////Console.WriteLine("Towary: {0}", Merch.Goods);            
            ////foreach(Merch t in Merch.Goods)
            ////    Console.WriteLine(t.ToString());


            //Galaxy gal = new Galaxy();
            //gal.Generate(2);
            //Console.WriteLine(gal);

            //Fleet f1 = new Fleet(gal.Planets[0]);
            //Fleet f2 = new Fleet(new Position(0.1,0.0));

            //gal.Planets[0].Dock(f1);
            ////gal.Planets[0].Dock(f2);

            //TradeMgmt.Deals.RegisterPurchase(gal.Planets[0], f1, Merch.Goods[2], 10);
            ////TradeMgmt.Deals.RegisterPurchase(gal.Planets[0], f2, Merch.Goods[0], 10);
            ////TradeMgmt.Deals.RegisterPurchase(gal.Planets[0], f2, Merch.Goods[0], 10);
            ////TradeMgmt.Deals.RegisterPurchase(gal.Planets[0], f2, Merch.Goods[0], 10);
            ////TradeMgmt.Deals.RegisterPurchase(gal.Planets[0], f2, Merch.Goods[0], 10);

            ////TradeMgmt.Deals.RegisterSale(f2, gal.Planets[0], Merch.Goods[0], 10);
            ////TradeMgmt.Deals.RegisterSale(f2, gal.Planets[0], Merch.Goods[0], 10);

            //Console.WriteLine(f1);

            //TradeMgmt.Deals.Commit();

            //Console.WriteLine(f1);
            //Console.WriteLine(f2);
            
            ////TradeMgmt.Deals.RegisterPurchase(gal.Planets[0], f2, Merch.Goods[2], 10);

            //f2.Aim = gal.Planets[0];

            //for (int i = 0; i < 7; i++)
            //{
            //    f2.Move();
            //    Console.WriteLine(f2);
            //}

            //TradeMgmt.Deals.RegisterPurchase(gal.Planets[0], f2, Merch.Goods[2], 10);
            //Console.WriteLine(f2);

            //Console.ReadLine();
        }





        static void LoadMerchandises()
        {
            try
            {
                using (StreamReader sr = new StreamReader("Merchandises.txt"))
                {
                    string line;
                    int i = 0;
                    while ((line = sr.ReadLine()) != null)
                    {
                        i++;
                        //Console.WriteLine(line);
                        string[] pola = line.Split(new Char[] { ' ', '\t', '\n' }, StringSplitOptions.RemoveEmptyEntries);
                        if (i == 1)
                            continue;

                        //foreach (string s in pola)
                        //    Console.WriteLine("{0}", s);
                        if (pola.Length < 6)
                        {
                            string txt = "WARNING: File Merchandises.txt, line " + i.ToString() + ", not enough arguments";
                            throw new ArgumentException(txt);
                        }
                        else
                        {
                            Merch.EType type;
                            if (pola[5] == "raw")
                                type = Merch.EType.Raw;
                            else if (pola[5] == "processed")
                                type = Merch.EType.Processed;
                            else
                                throw new ArgumentException("Invalid product type:"+pola[5]);


                            Merch.Goods.Add(new Merch(pola[0], int.Parse(pola[1]), int.Parse(pola[2]), int.Parse(pola[3]), int.Parse(pola[4]),type));
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Problem with file Merchantise.txt:");
                Console.WriteLine(e.Message);
                throw;
            }


        }
    }
}
